package cn.wolfcode.controller;

import cn.wolfcode.entity.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author y
 * @since 2025-08-19
 */
@RestController
@CrossOrigin
public class EmployeeController {

    public static Map<String, Object> map = new HashMap<>();

    @Autowired
    private EmployeeMapper employeeMapper;

    @RequestMapping("/login")
    public Employee login(String email, String password, HttpServletRequest request){
        /*
            验证用户名密码合法性的sql语句
            SELECT *
            from employee
            where email = 'admin@abc.com' and password='1'
        * */
        //构建一个条件构造器。在复杂条件查询（不用主键查询，用其他条件）时，使用这个对象
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //eq为等值条件，当前的语句表示使用用户输入的email作为查询条件，查询数据库的email列
        queryWrapper.eq("email", email);
        queryWrapper.eq("password", password);

        Employee employee = employeeMapper.selectOne(queryWrapper);
        if(employee == null){
           employee = new Employee();
           employee.setId(-1l);
        }else{
            //登录成功，记录登录状态。就是将当前用户记录在服务器的session中
            String ip = request.getRemoteAddr();
            map.put(ip, employee);
        }
        return employee;
    }

}
